<template>
  <view 
    v-if="show"
    class="fu-notify"
    :class="[
      `fu-notify--${type}`,
      { 'fu-notify--with-icon': showIcon }
    ]"
    :style="{ top: safeAreaInsetTop }"
  >
    <!-- 图标 -->
    <text 
      v-if="showIcon"
      class="fu-notify__icon fu-icon"
    >{{ iconMap[type] }}</text>
    
    <!-- 文本内容 -->
    <text class="fu-notify__text">{{ message }}</text>
  </view>
</template>

<script>
/**
 * fu-notify 消息通知组件
 * @description 在页面顶部展示消息通知
 * @property {Boolean} show - 是否显示
 * @property {String} message - 通知文本内容
 * @property {String} type - 主题类型，可选值为 primary success warning error
 * @property {Boolean} showIcon - 是否显示图标
 * @property {Number} duration - 展示时长(ms)，值为 0 时不会自动关闭
 * @property {String} safeAreaInsetTop - 顶部安全区域高度
 * @event {Function} close - 关闭时触发
 */
export default {
  name: 'fu-notify',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    message: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      default: 'primary',
      validator: value => ['primary', 'success', 'warning', 'error'].includes(value)
    },
    showIcon: {
      type: Boolean,
      default: true
    },
    duration: {
      type: Number,
      default: 3000
    },
    safeAreaInsetTop: {
      type: String,
      default: '0px'
    }
  },
  emits: ['update:show', 'close'],
  data() {
    return {
      iconMap: {
        primary: '&#xe6e3;',
        success: '&#xe6e4;',
        warning: '&#xe6e6;',
        error: '&#xe6e8;'
      },
      timer: null
    }
  },
  watch: {
    show(val) {
      if (val) {
        this.clearTimer()
        if (this.duration > 0) {
          this.timer = setTimeout(() => {
            this.onClose()
          }, this.duration)
        }
      }
    }
  },
  beforeUnmount() {
    this.clearTimer()
  },
  methods: {
    clearTimer() {
      if (this.timer) {
        clearTimeout(this.timer)
        this.timer = null
      }
    },
    onClose() {
      this.clearTimer()
      this.$emit('update:show', false)
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>
.fu-notify {
  position: fixed;
  left: 0;
  right: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24rpx 32rpx;
  font-size: 28rpx;
  line-height: 1.5;
  animation: fu-slide-down 0.3s both;
  
  // 主题样式
  &--primary {
    background-color: var(--fu-primary-color);
    color: #fff;
  }
  
  &--success {
    background-color: var(--fu-success-color);
    color: #fff;
  }
  
  &--warning {
    background-color: var(--fu-warning-color);
    color: #fff;
  }
  
  &--error {
    background-color: var(--fu-danger-color);
    color: #fff;
  }
  
  &--with-icon {
    justify-content: flex-start;
  }
  
  &__icon {
    font-family: 'fuIcon';
    font-size: 36rpx;
    margin-right: 12rpx;
  }
  
  &__text {
    word-break: break-all;
  }
}

@keyframes fu-slide-down {
  from {
    transform: translateY(-100%);
  }
  to {
    transform: translateY(0);
  }
}
</style> 